#include"string.h"

namespace my
{
	const size_t string::npos = -1;

	string::string(const char* str)
		:_size(strlen(str))
	{
		_str = new char[_size + 1];
		_capacity = _size;

		strcpy(_str, str);
	}

	string::string(const string& s)
	{
		_str = new char[s._capacity + 1];
		strcpy(_str, s._str);
		_capacity = s._capacity;
		_size = s._size;
	}

	string& string::operator=(const string& s)
	{
		if (&s != this)
		{
			_str = new char[s._capacity + 1];
			strcpy(_str, s._str);
			_size = s._size;
			_capacity = s._capacity;
		}

		return *this;
	}

	void string::reserve(size_t n)
	{
		if (n > _capacity)
		{
			char* temp = new char[n + 1];
			strcpy(temp, _str);
			delete[] _str;
			_str = temp;
			_str[n] = '\0';
			_capacity = n;
		}
	}

	void string::resize(size_t n)
	{
		if (n < _size)
		{
			_str[n] = '\0';
			_size = n;
		}
		else
		{
			reserve(n);
		}
	}

	void string::push_back(char c)
	{
		if (_size == _capacity)
		{
			reserve(_capacity > 0 ? 2 * _capacity : 8);
		}

		_str[_size++] = c;
	}

	void string::append(const char* s)
	{
		int len = strlen(s);
		if (len + _size > _capacity)
		{
			reserve((len + _size) > 2 * _capacity ? len + _size : 2 * _capacity);
		}

		strcpy(_str + _size, s);
		_size += len;
	}

	void string::clear()
	{
		_str[0] = '\0';
		_size = 0;
	}

	bool string::empty() const
	{
		return !_size;
	}

	string& string::operator+=(const char* s)
	{
		append(s);
		return *this;
	}

	string& string::operator+=(char c)
	{
		push_back(c);
		return *this;
	}

	void string::insert(size_t pos, char ch)
	{
		assert(pos < _size);

		if (_size == _capacity)
		{
			reserve(_capacity == 0 ? 4 : _capacity * 2);
		}

		size_t end = _size;
		while (end > pos)
		{
			_str[end] = _str[end - 1];
 			end--;
		}
		_str[pos] = ch;
		_size++;
	}
	void string::insert(size_t pos, const char* str)
	{
		assert(pos < _size);

		int len = strlen(str);
		if (_size + len > _capacity)
		{
			reserve(len + _size > 2 * _capacity ? len + _size : _capacity * 2);
		}

		size_t end = _size + len - 1;
		while (end > pos + len - 1)
		{
			_str[end] = _str[end - len];
			end--;
		}
		
		for (size_t i = 0; i < len; i++)
		{
			_str[i + pos] = str[i];
		}
		_size += len;
	}
	void string::erase(size_t pos, size_t len)
	{
		assert(pos < _size);

		if (len > _size - pos)
		{
			len = _size - pos;
		}

		for (size_t i = pos + len; i < _size; i++)
		{
			_str[i - len] = _str[i];
		}

		_str[_size - len] = '\0';
		_size -= len;
	}

	string string::substr(size_t pos, size_t len) const
	{
		assert(pos < _size);

		if (len > pos - _size)
		{
			len = pos - _size;
		}

		string ret;
		ret.reserve(len);
		for (size_t i = 0; i < len; i++)
		{
			ret += _str[i + pos];
		}
		
		return ret;
	}

	size_t string::find(char ch, size_t pos) const
	{
		assert(pos < _size);

		for (size_t i = pos; i < _size; i++)
		{
			if (_str[i] == ch)
			{
				return i;
			}
		}
		return npos;
	}
	size_t string::find(const char* str, size_t pos) const
	{
		assert(pos < _size);

		size_t len = strlen(str);
		
		for (size_t i = pos; i < _size; i++)
		{
			if (_str[i] == str[0])
			{
				int count = 1;
				int j = 1;
				for (size_t l = i + 1; l < _size; l++)
				{
					if (_str[l] == str[j++])
					{
						count++;
					}
					else
					{
						break;
					}
				}
				if (count == len)
				{
					return i;
				}
			}
		}
		return npos;
	}

	void string::swap(string& s)
	{
		std::swap(_str, s._str);
		std::swap(_size, s._size);
		std::swap(_capacity, s._capacity);
	}

	void swap(string& x, string& y)
	{
		x.swap(y);
	}

	bool operator== (const string& lhs, const string& rhs)
	{
		if (strcmp(lhs.c_str(), rhs.c_str()) == 0)
		{
			return true;
		}
		else
		{
			return false;
		}
	}

	bool operator!= (const string& lhs, const string& rhs)
	{
		if (lhs == rhs)
		{
			return false;
		}
		else
		{
			return true;
		}
	}

	bool operator> (const string& lhs, const string& rhs)
	{
		if (strcmp(lhs.c_str(), rhs.c_str()) > 0)
		{
			return true;
		}
		else
		{
			return false;
		}
	}

	bool operator< (const string& lhs, const string& rhs)
	{
		if (strcmp(lhs.c_str(), rhs.c_str()) < 0)
		{
			return true;
		}
		else
		{
			return false;
		}
	}

	bool operator>= (const string& lhs, const string& rhs)
	{
		if (lhs > rhs || lhs == rhs)
		{
			return true;
		}
		else
		{
			return false;
		}
	}

	bool operator<= (const string& lhs, const string& rhs)
	{
		if (lhs < rhs || lhs == rhs)
		{
			return true;
		}
		else
		{
			return false;
		}
	}

	ostream& operator<<(ostream& os, const string& str)
	{
		for (size_t i = 0; i < str.size(); i++)
		{
			os << str[i];
		}
		return os;
	}

	istream& operator>>(istream& is, string& str)
	{
		str.clear();

		int i = 0;
		char buff[256];

		char ch = is.get();
		while (ch != '\n')
		{
			buff[i++] = ch;
			if (i == 255)
			{
				buff[i] = '\0';
				str += buff;
				i = 0;
			}

			ch = is.get();
		}

		if (i > 0)
		{
			buff[i] = '\0';
			str += buff;
		}
		return is;
	}

	istream& getline(istream& is, string& str, char delim)
	{
		str.clear();

		int i = 0;
		char buff[256];

		char ch = is.get();
		while (ch != delim)
		{
			buff[i++] = ch;
			if (i == 255)
			{
				buff[i] = '\0';
				str += buff;
				i = 0;
			}

			ch = is.get();
		}

		if (i > 0)
		{
			buff[i] = '\0';
			str += buff;
		}
		return is;
	}

	string::string(const char* str)
	{
		_str = new char[strlen(str) + 1];
		strcpy(_str, str);
	}

	string::string(const string& s)
	{
		_str = new char[strlen(s._str) + 1];
		strcpy(_str, s._str);
	}

	string& string::operator=(const string& s)
	{
		if (&s != this)
		{
			_str = new char[strlen(s._str) + 1];
			strcpy(_str, s._str);
		}

		return *this;
	}

	string::~string()
	{
		delete[] _str;
	}
}